﻿using StackExchange.Redis;

namespace Fetches.Fetch.Utils.KvStore
{
    /// <summary>
    /// 页面资源url到二进制存储的KV映射，实际应该是接口并有本地和分布式实现的
    /// </summary>
    public class RedisKvStore:IKvStore
    {
        private ConnectionMultiplexer _redis;
        private string MapKey = "Map";
        
        public void Initialize(string config, long uniqueId)
        {
            _redis = ConnectionMultiplexer.Connect(config);
            MapKey = MapKey + uniqueId;
        }

        public bool Exists(string key)
        {
            var db = _redis.GetDatabase();
            return db.HashExists(MapKey, key);
        }

        public void Put(string key, string value)
        {
            var db = _redis.GetDatabase();
            db.HashSet(MapKey, key, value);
        }

        public string Get(string key)
        {
            var db = _redis.GetDatabase();
            return db.HashGet(MapKey, key);
        }

        public void Dispose()
        {
            _redis.Dispose();
        }
    }
}
